package com.wf.articleManagement.config;

import org.apache.http.HttpHost;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.Arrays;

/**
 * @Description: ES配置类
 * 主要解决ES出现如下错误 ： Caused by: java.net.SocketTimeoutException: 60 milliseconds timeout on connection http-outgoing-3 [ACTIVE]
 */
@Configuration
public class RestClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {

        HttpHost node1 = new HttpHost("124.222.113.171",9200,"http");
        HttpHost node2 = new HttpHost("124.222.113.171",9201,"http");
        HttpHost node3 = new HttpHost("124.222.113.171",9202,"http");

        RestClientBuilder rclientBuilder = RestClient.builder(node1,node2,node3)
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
                    }
                });
       return  new RestHighLevelClient(rclientBuilder);
    }
}
